package cn.catsdad.controller;

import cn.catsdad.base.controller.BaseCrudController;
import cn.catsdad.base.web.HttpResponse;
import cn.catsdad.dto.DocumentDTO;
import cn.catsdad.page.DocumentParam;
import cn.catsdad.service.api.DocumentService;
import cn.catsdad.vo.UploadVo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件管理
 *
 * @author xuyonglong
 * @version V1.0 2023/4/25
 */
@RestController
@RequestMapping("/api/system/documents")
public class DocumentController extends BaseCrudController<DocumentDTO, Long, DocumentService, DocumentParam> {

    /**
     * 上传
     *
     * @param uploadVo 文件传输对象
     * @return 通用返回结果
     */
    @PostMapping("/uploads")
    public HttpResponse<List<DocumentDTO>> upload(UploadVo uploadVo) {
        return new HttpResponse<List<DocumentDTO>>()
                .setData(service.uploadFiles(uploadVo));
    }

    /**
     * 下载文件
     *
     * @param id       文件id
     * @param response 响应流
     */
    @GetMapping("/download/{id}")
    public void download(@PathVariable("id") Long id, HttpServletResponse response) {
        service.downloadFile(id, response);
    }

}
